﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FCNS.Utility;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoney.UI;
using FCNS.KeepMoney.Data;

namespace FCNS.KeepMoney.Forms
{
    public partial class OptionForm : ParentForm
    {
        AceMoney configMoney = Program.AppConfig.Money;
        AceApplication configApp = Program.AppConfig.App;
        AceMainForm configMainForm = Program.AppConfig.MainForm;
        AceGtd configGtd = Program.AppConfig.Gtd;
        AceChart configChart = Program.AppConfig.Charts;
        AceBill configBill = Program.AppConfig.Bill;

        private CheckedLVItemDXList cdxGeneral = null;
        private CheckedLVItemDXList cdxGui = null;
        Keys k1 = Keys.None;
        Keys k2 = Keys.None;

        public OptionForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            BannerFactory.CreateBannerEx(this, pictureBoxLogo, Properties.Resources.option32,
                LanguageUtil.选项, LanguageUtil.在这里你可以配置KeepMoney全局程序选项);

            LoadOption();

            this.comboBoxGtdBackgroupImage.SelectedIndexChanged += new System.EventHandler(this.OnSelectedBackgroundImage);
        }

        private void LoadOption()
        {
            LoadIntegration();
            LoadAdvanced();
            LoadGeneral();
            LoadChart();
            LoadBill();
            LoadGtd();
        }

        private void LoadGtd()
        {
            comboBoxGtdBackgroupImage.Items.Add(LanguageUtil.无);
            comboBoxGtdBackgroupImage.Items.Add(LanguageUtil.默认);
            comboBoxGtdBackgroupImage.Items.Add(LanguageUtil.自定义);
            comboBoxGtdBackgroupImage.SelectedIndex = configGtd.BackgroundImage;
            if (comboBoxGtdBackgroupImage.SelectedIndex == 1)
                pictureBoxGtd.Image = Properties.Resources.gtd;
            else if (comboBoxGtdBackgroupImage.SelectedIndex == 2)
                pictureBoxGtd.Image = Image.FromFile(AppDefines.gtdBackgroupImage);

            comboBoxGtdBackgroupImage.SelectedIndexChanged += new EventHandler(OnSelectedBackgroundImage);
        }

        private void LoadChart()
        {
            checkBoxChartShowZero.Checked = configChart.HideZero;
            checkBoxLineMode.Checked = configChart.LineMode;
            checkBoxChart3D.Checked = configChart.Enable3D;
            checkBoxExpensesDisplayedAsNegative.Checked = configChart.ExpensesDisplayedAsNegative;
            numericUpDownPointDepth.Value = configChart.PointDepth;
            numericUpDownGapDepth.Value = configChart.GapDepth;
        }

        private void LoadBill()
        {
            numericUpDownBillDay.Value = configBill.BillDay;
            comboBoxBillMode.Items.AddRange(Enum.GetNames(typeof(BillsMode)));
            comboBoxBillMode.SelectedIndex = (int)configBill.BillMode;

            comboBoxBillStyle.Items.Add(LanguageUtil.默认);
            foreach (string file in System.IO.Directory.GetFiles(AppDefines.billDirectory))
                comboBoxBillStyle.Items.Add(System.IO.Path.GetFileName(file));

            comboBoxBillStyle.Text = configBill.BillStyle;
        }

        private void LoadIntegration()
        {
            k1 = (Keys)configApp.HotkeyEntry;
            hotKeyControlEx1.HotKey = (k1 & Keys.KeyCode);
            hotKeyControlEx1.HotKeyModifiers = (k1 & Keys.Modifiers);
            hotKeyControlEx1.RenderHotKey();

            k2 = (Keys)configApp.HotkeyForm;
            hotKeyControlEx2.HotKey = (k2 & Keys.KeyCode);
            hotKeyControlEx2.HotKeyModifiers = (k2 & Keys.Modifiers);
            hotKeyControlEx2.RenderHotKey();

            checkBoxAutoRun.Checked = configApp.AutoRun;
            checkBoxMsg.Checked = configApp.EnablAlertMessage;
            checkBoxLoginForm.Checked = configApp.DisplayTheLoginForm;
            checkBoxPrompt.Checked = configApp.DisplayThePromptForm;
            checkBoxEncryptionData.Checked = configApp.EncryptionData;
        }

        private void LoadGeneral()
        {
            listViewExGeneral.Columns.Add(LanguageUtil.常规, 400);

            ListViewGroup lvg1 = new ListViewGroup(LanguageUtil.主窗体);
            listViewExGeneral.Groups.Add(lvg1);
            cdxGui = new CheckedLVItemDXList(listViewExGeneral);
            cdxGui.CreateItem(configMainForm, "MinimizeToTray", lvg1, LanguageUtil.最小化窗体到系统状态栏);
            cdxGui.CreateItem(configMainForm, "CloseToTray", lvg1, LanguageUtil.关闭程序时最小化到系统状态栏);
            cdxGui.CreateItem(configMainForm, "DisplaysTheFilePathInTheMainFormText", lvg1, LanguageUtil.在标题栏显示数据库路径);

            ListViewGroup lvg2 = new ListViewGroup(LanguageUtil.视图);
            listViewExGeneral.Groups.Add(lvg2);
            cdxGui.CreateItem(configMainForm, "GridLine", lvg2, LanguageUtil.显示网格线);
            cdxGui.CreateItem(configMoney, "ShowPlus", lvg2, LanguageUtil.在收入数值前显示正号);
            cdxGui.CreateItem(configMoney, "ShowMinus", lvg2, LanguageUtil.在支出数值前显示负号);

            ListViewGroup lvg3 = new ListViewGroup(LanguageUtil.数据);
            listViewExGeneral.Groups.Add(lvg3);
            cdxGeneral = new CheckedLVItemDXList(listViewExGeneral);
            cdxGeneral.CreateItem(configApp, "AutoOpenKMD", lvg3, LanguageUtil.自动加载最后使用的数据库文件);
            cdxGeneral.CreateItem(configMoney, "ValidationBalance", lvg3, LanguageUtil.钱袋金额不足禁止支出);
            //   cdxGeneral.CreateItem(configMoney, "PurseBalanceCanNotModify", lvg, LanguageUtil.钱袋余额不能修改);
            cdxGeneral.CreateItem(configBill, "LockBill", lvg3, LanguageUtil.禁止修改已生成账单的数据);
            cdxGeneral.CreateItem(configApp, "FirstDayIsSunday", lvg3, LanguageUtil.设置星期第一天是星期天而不是星期一);

            cdxGeneral.UpdateData(false);
        }

        private void LoadAdvanced()
        {
            numericUpDownDigits.Value = configMoney.DecimalPlaces;
            numericUpDownPrecision.Value = configMoney.Precision;
            numericUpDownCappedAmount.Value = configMoney.CappedAmount;
            numericUpDownCappedAmount.DecimalPlaces = configMoney.DecimalPlaces;

            comboBoxDateStyle.Items.Add(LanguageUtil.当前格式);
            comboBoxDateStyle.Items.Add(LanguageUtil.日期星期);
            comboBoxDateStyle.Items.Add(LanguageUtil.日期);
            comboBoxDateStyle.Items.Add(LanguageUtil.日期时间);
            comboBoxDateStyle.SelectedIndex = 0;

            buttonLineColor.BackColor = Color.FromArgb(configMainForm.AlternateRowBackColor);
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            cdxGeneral.UpdateData(true);
            cdxGui.UpdateData(true);

            //整合
            ChangeHotKey(ref k1, hotKeyControlEx1, AppDefines.hotkeyNewEntry);
            ChangeHotKey(ref k2, hotKeyControlEx2, AppDefines.hotkeyShowMainForm);

            if (checkBoxAutoRun.Checked != configApp.AutoRun)
            {
                configApp.AutoRun = checkBoxAutoRun.Checked;
                WinUtil.SetStartWithWindows(checkBoxAutoRun.Checked, AppDefines.appPath + AppDefines.fullName);
            }

            configApp.EnablAlertMessage = checkBoxMsg.Checked;
            configApp.DisplayTheLoginForm = checkBoxLoginForm.Checked;
            configApp.DisplayThePromptForm = checkBoxPrompt.Checked;
            configApp.EncryptionData = checkBoxEncryptionData.Checked;

            //高级
            configMoney.DecimalPlaces = decimal.ToInt32(numericUpDownDigits.Value);
            configMoney.Precision = decimal.ToInt32(numericUpDownPrecision.Value);
            configMoney.CappedAmount = numericUpDownCappedAmount.Value;

            switch (comboBoxDateStyle.SelectedIndex)
            {
                case 1:
                    configApp.DateFormat = "yyyy-MM-dd  dddd";
                    break;
                case 2:
                    configApp.DateFormat = "yyyy-MM-dd";
                    break;
                case 3:
                    configApp.DateFormat = "yyyy-MM-dd HH:mm:ss";
                    break;
            }

            //账单
            configBill.BillDay = decimal.ToInt32(numericUpDownBillDay.Value);
            configBill.BillMode = (BillsMode)comboBoxBillMode.SelectedIndex;
            //conBill.BillStyle = (BillsStyle)comboBoxBillStyle.SelectedIndex;

            //图表
            configChart.HideZero = checkBoxChartShowZero.Checked;
            configChart.LineMode = checkBoxLineMode.Checked;
            configChart.Enable3D = checkBoxChart3D.Checked;
            configChart.ExpensesDisplayedAsNegative = checkBoxExpensesDisplayedAsNegative.Checked;
            configChart.PointDepth = (int)numericUpDownPointDepth.Value;
            configChart.GapDepth = (int)numericUpDownGapDepth.Value;                    

            //GTD
            configGtd.BackgroundImage = comboBoxGtdBackgroupImage.SelectedIndex;

            this.DialogResult = DialogResult.OK;
        }

        private void ChangeHotKey(ref Keys kPrevHK, FCNS.Controls.HotKeyControlEx hkControl, int nHotKeyID)
        {
            Keys kNew = (hkControl.HotKey | hkControl.HotKeyModifiers);
            if (kPrevHK != kNew)
            {
                kPrevHK = kNew;

                if (nHotKeyID == AppDefines.hotkeyNewEntry)
                    configApp.HotkeyEntry = (ulong)kNew;
                else if (nHotKeyID == AppDefines.hotkeyShowMainForm)
                    configApp.HotkeyForm = (ulong)kNew;

                WinUtil.UnregisterHotKey(Program.TheMainForm.Handle, nHotKeyID);
                if (kPrevHK != Keys.None)
                    WinUtil.RegisterHotKey(Program.TheMainForm.Handle, nHotKeyID, kPrevHK);
            }
        }

        private void OnBillModeChange(object sender, EventArgs e)
        {
            numericUpDownBillDay.Enabled = comboBoxBillMode.SelectedIndex ==
                (int)BillsMode.Bill ? true : false;
        }

        //GTD
        private void OnSelectedBackgroundImage(object sender, EventArgs e)
        {
            if (comboBoxGtdBackgroupImage.SelectedIndex == 2)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp";
                    if (ofd.ShowDialog() == DialogResult.OK)
                        System.IO.File.Copy(ofd.FileName, AppDefines.gtdBackgroupImage, true);
                    else
                        comboBoxGtdBackgroupImage.SelectedIndex = 0;
                }
            }
        }

        private void OnChangeLineColor(object sender, EventArgs e)
        {
            using (ColorDialog cd = new ColorDialog())
            {
                cd.AllowFullOpen = true;
                cd.AnyColor = true;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    buttonLineColor.BackColor = cd.Color;
                    configMainForm.AlternateRowBackColor = cd.Color.ToArgb();
                }

            }
        }

        private void m_btnFileExtCreate_Click(object sender, EventArgs e)
        {
            FCNS.Utility.ShellUtil.RegisterExtension(AppDefines.fileExt, AppDefines.extId, AppDefines.fullName, ProgramUtil.GetExecutable(), AppDefines.shortName);
        }

        private void m_btnFileExtRemove_Click(object sender, EventArgs e)
        {
            ShellUtil.UnregisterExtension(AppDefines.fileExt, AppDefines.extId);
        }

        private void checkBoxChart3D_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownGapDepth.Enabled = checkBoxChart3D.Checked;
            numericUpDownPointDepth.Enabled = checkBoxChart3D.Checked;
        }
    }
}